package com.zzjc.web.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzjc.web.entity.Device;
import com.zzjc.web.mapper.DeviceDao;
import com.zzjc.web.service.DeviceService;
import com.zzjc.web.util.ApiResponse;
import com.zzjc.web.util.DateUtils;
import com.zzjc.web.util.ResponseUtil;
import com.zzjc.web.util.StringKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceDao deviceDao;

    @Override
    public ApiResponse addDevice(Device device) {
        try {
            Device existingDevice = deviceDao.getDeviceById(device.getDeviceId());
            if (existingDevice != null) {
                return ResponseUtil.error("设备已存在,无需重复添加");
            }
            device.setCreateTime(DateUtils.getNowDate());
            int result = deviceDao.insertDevice(device);
            return ResponseUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error("添加设备失败请重试");
        }
    }

    @Override
    public ApiResponse updateDevice(Device device) {
        if (StringKit.notBlank(device.getDeviceId())) {
            try {
                if (deviceDao.getDeviceById(device.getDeviceId()) == null) {
                    return ResponseUtil.error("设备不存在");
                } else {
//                    Device existingDevice = deviceDao.getDeviceById(device.getDeviceId());
//                    if (existingDevice != null) {
//                        return ResponseUtil.error("该序列号已存在,不能重复添加");
//                    }
                    device.setUpdateTime(DateUtils.getNowDate());
                    int result = deviceDao.updateDevice(device);
                    return ResponseUtil.success();
                }
            } catch (Exception e) {
                e.printStackTrace();
                return ResponseUtil.error("修改设备失败");
            }
        } else {
            return ResponseUtil.error("设备id不能为空");
        }
    }

    @Override
    public ApiResponse deleteDevice(List<String> deviceList) {
        if (deviceList.size() > 0) {
            try {
                List<Device> deviceById = deviceDao.getDeviceByList(deviceList);
                if (deviceById.size() < 1) {
                    return ResponseUtil.error("设备不存在");
                } else {
                    deviceDao.deleteDevice(deviceList);
                    return ResponseUtil.success();
                }
            } catch (Exception e) {
                e.printStackTrace();
                return ResponseUtil.error("删除设备失败");
            }
        } else {
            return ResponseUtil.error("id不能为空");
        }
    }

    @Override
    public ApiResponse getAllDevices(Device device) {
        PageInfo<Device> pageInfo = null;
        try {
            int pageNum = 1;
            if (device.getPageNum() != null) {
                pageNum = device.getPageNum();
            }
            int pageSize = 1000;
            if (device.getPageSize() != null) {
                pageSize = device.getPageSize();
            }
            PageHelper.startPage(pageNum, pageSize);
            List<Device> deviceList = deviceDao.findAll(device);
            pageInfo = new PageInfo<>(deviceList);
            return ResponseUtil.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error("查询失败，请重试");
        }
    }
} 